#!/usr/bin/env python
# coding: utf-8
# $Id$
# http request producer

import parallels
from twisted.internet import defer, reactor
from ex2consumer import __file__ as consumer

def response(item):
    print 'information for %s on "freegeoip.net":\n%s\n' % (item['host'], item['content'])
    reactor.stop()

def main():
    # Here we just request Parallels to execute 'ex2consumer.py'.
    # I prefer to import the __file__ attribute because it will
    # prevent errors (such as syntax errors, etc) during the 
    # Consumer's runtime.
    producer = parallels.Parallels(consumer)

    # Then I send this item to the consumer, and wait for the reply.
    # Once the replied data arrives, it will fire the response()
    # method previously set in that deferred.
    item = {'host': 'twistedmatrix.com', 'addr': '66.35.39.65'}
    deferred = producer.put(item)
    deferred.addCallback(response)

if __name__ == '__main__':
    reactor.callWhenRunning(main)
    reactor.run()
